$(function() {
	var oCal = $('#calendar');
	var oTitle = $('.center-date');
	var aTd = oCal.find('td');
	var dayNum = '';
	var pretBut = $('.pre-date');
	var nextBut = $('.next-date');
	var oDate;
	var preYear;
	var preMonth;
	
	$(".close").click(function(){
		$(".modal").hide();
	})

	$(".gzsj").click(function() {
		var doctorId = $(this).attr("doctorId");
		$("#doctorId").val(doctorId);
		$(".modal").show();
		oDate = new Date();
		preYear = oDate.getFullYear();
		preMonth = oDate.getMonth() + 1;
		preDay = oDate.getDate();
		oTitle.find('span').eq(0).html(preYear);
		oTitle.find('span').eq(1).html(preMonth);

		if (preMonth == 1) {
			showDate(preYear, 12);
		} else {
			showDate(preYear, preMonth);
		}
		workDate(preDay);
		//showColor();
		showClik();
	});

	$(".today").click(function() {
		oDate = new Date();
		preYear = oDate.getFullYear();
		preMonth = oDate.getMonth() + 1;
		preDay = oDate.getDate();
		oTitle.find('span').eq(0).html(preYear);
		oTitle.find('span').eq(1).html(preMonth);

		if (preMonth == 1) {
			showDate(preYear, 12);
		} else {
			showDate(preYear, preMonth);
		}
		workDate(preDay);
		//showColor(preDay);
		showClik();
	});

	$(".mc-calendar").click(function() {
		oDate = new Date();
		$(".calendar").fadeIn();
		$(".modal-backdrop").fadeIn();
		preYear = oDate.getFullYear();
		preMonth = oDate.getMonth() + 1;
		preDay = oDate.getDate();
		oTitle.find('span').eq(0).html(preYear);
		oTitle.find('span').eq(1).html(preMonth);
		if (preMonth == 1) {
			showDate(preYear, 12);
		} else {
			showDate(preYear, preMonth);
		}
		workDate(preDay);
		//showColor(preDay);
		showClik();
	});

	function showDate(year, month) {
		var oDate = new Date();
		dayNum = '';
		oCal.html('');
		oTitle.html('<span>' + year + '</span>' + '年' + '<span>' + isNum(month)
				+ '</span>' + '月');

		var arr = [ '周日', '周一', '周二', '周三', '周四', '周五', '周六', ];
		// 星期
		oCal.append('<table class="cale"><thead></thead><table>');
		for (var i = 0; i < arr.length; i++) {
			if (i == 6 || i == 0) {
				oCal.find('thead').append(
						'<th class="blue">' + arr[i] + '</th>');
			} else {
				oCal.find('thead').append('<th>' + arr[i] + '</th>');
			}
		}

		$('.cale').append('<tbody></tbody>');

		for (var i = 0; i < 6; i++) {
			oCal.find('tbody').append('<tr></tr>');
		}
		for (var j = 0; j < 7; j++) {
			oCal.find('tbody tr').append('<td><a href="#"></a><span style="display:inline-block;position:absolute; left:10px;bottom:5px"></span></td>');
		}
		for (var i = 0; i < $('td').length; i++) {
			aTd.eq(i).find('a').text('');
		}

		// 背景色
		oCal.find("tr").each(function(i) {
			var td_len = $(this).find("td").length;
			for (var i = 0; i < td_len; i++) {
				if (i % 2 == 0) {
					$(this).find("td").eq(i).css({
						"background" : "#fff6ef"
					});
				}
			}
		})
		// 判断当月多少天
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			dayNum = 31;
		} else if (month == 4 || month == 6 || month == 9 || month == 11) {
			dayNum = 30;
		} else if (month == 2 && isYear(year)) {
			dayNum = 29;
		} else {
			dayNum = 28;
		}

		oDate.setFullYear(year);
		oDate.setMonth(month - 1);
		oDate.setDate(1);
		switch (oDate.getDay()) {
		case 0:
			for (var i = 0; i < dayNum; i++) {
				oCal.find('tbody').find('td').eq(i).find('a').html(i + 1);
				oCal.find('tbody').find('td').eq(i).attr('date',
						year + '-' + isNum(month) + '-' + isNum(i + 1));
			}
			break;

		case 1:
			for (var i = 0; i < dayNum; i++) {
				oCal.find('tbody').find('td').eq(i + 1).find('a').html(i + 1);
				oCal.find('tbody').find('td').eq(i + 1).attr('date',
						year + '-' + isNum(month) + '-' + isNum(i + 1));
			}
			break;

		case 2:
			for (var i = 0; i < dayNum; i++) {
				oCal.find('tbody').find('td').eq(i + 2).find('a').html(i + 1);
				oCal.find('tbody').find('td').eq(i + 2).attr('date',
						year + '-' + isNum(month) + '-' + isNum(i + 1));
			}
			break;

		case 3:
			for (var i = 0; i < dayNum; i++) {
				oCal.find('tbody').find('td').eq(i + 3).find('a').html(i + 1);
				oCal.find('tbody').find('td').eq(i + 3).attr('date',
						year + '-' + isNum(month) + '-' + isNum(i + 1));
			}
			break;

		case 4:
			for (var i = 0; i < dayNum; i++) {
				oCal.find('tbody').find('td').eq(i + 4).find('a').html(i + 1);
				oCal.find('tbody').find('td').eq(i + 4).attr('date',
						year + '-' + isNum(month) + '-' + isNum(i + 1));
			}
			break;

		case 5:
			for (var i = 0; i < dayNum; i++) {
				oCal.find('tbody').find('td').eq(i + 5).find('a').html(i + 1);
				oCal.find('tbody').find('td').eq(i + 5).attr('date',
						year + '-' + isNum(month) + '-' + isNum(i + 1));
			}
			break;

		case 6:
			for (var i = 0; i < dayNum; i++) {
				oCal.find('tbody').find('td').eq(i + 6).find('a').html(i + 1);
				oCal.find('tbody').find('td').eq(i + 6).attr('date',
						year + '-' + isNum(month) + '-' + isNum(i + 1));
			}
			break;
		}
	}

	pretBut.click(function() {
		preYear = parseInt(oTitle.find('span').eq(0).html());
		preMonth = parseInt(oTitle.find('span').eq(1).html());
		if (preMonth == 1) {
			showDate(preYear - 1, 12);
		} else {
			showDate(preYear, preMonth - 1);
		}
		workDate(preDay);
		//showColor(preDay);
		showClik();
	});

	nextBut.click(function() {
		preYear = parseInt(oTitle.find('span').eq(0).html());
		preMonth = parseInt(oTitle.find('span').eq(1).html());
		if (preMonth == 12) {
			showDate(preYear + 1, 1);
		} else {
			showDate(preYear, preMonth + 1);
		}
		workDate(preDay);
		//showColor(preDay);
		showClik();
	});

	function workDate(preDay){
		var doctorId = $("#doctorId").val();
		$.ajax({
			type:"post",
			url:ctx+"/doctorWork/index",
			data:{"doctorId":doctorId},
			dataType:"json",
			success:function(data){
				var dates = "";
				$(data).each(function(i){
					dates += "'"+data[i]["workDate"]+"',";
				});
				console.info(dates)
				showColor(preDay,dates);
			}
		});
	}
	
	function showColor(preDay,dates) {
		var oDate = new Date();
		var oCals = document.getElementById('calendar');
		var aTds = oCals.getElementsByTagName('td');
		var result = [];
		var index = 0;

		for (var i = 0; i < aTds.length; i++) {
			var aA = aTds[i].getElementsByTagName('a')[0];
			if (aA.innerHTML != '') {
				var date = $("#calendar td").eq(i).attr("date");
				if(dates.indexOf("'"+date+"'") >= 0){
					$("#calendar td").eq(i).find("span").html("班");
				}
				result.push(aTds[i]);
			}
		}
		if ($('.center-date').find('span').eq(0).html() < oDate.getFullYear()) {
			//小于年，所有为灰色
			for (var i = 0; i < aTds.length; i++) {
				aTds[i].className = '';
			}
		} else if ($('.center-date').find('span').eq(0).html() == oDate
				.getFullYear()
				&& $('.center-date').find('span').eq(1).html() == oDate
						.getMonth() + 1) {
			//若页面日期为当前年月
			for (var i = 0; i < result.length; i++) {
				var aHtml = result[i].getElementsByTagName('a')[0].innerHTML;
				if (aHtml == preDay) {
					result[i].className = 'red';
					
					$(".red").find("a").html("今");
					index = i;
				}
			}
			for (var i = index + 1; i < dayNum; i++) {
				result[i].className = 'blue';
			}
		} else if ($('.center-date').find('span').eq(0).html() > oDate
				.getFullYear()
				|| $('.center-date').find('span').eq(1).html() > oDate
						.getMonth()) {
			//若页面超出当前年月
			for (var i = 0; i < aTds.length; i++) {
				aTds[i].className = 'blue';
			}
		}
	}
	;
	function isYear(year) {
		if (year % 4 == 0 || year % 100 != 0) {
			return true;
		} else {
			if (year % 400 == 0) {
				return true;
			} else {
				return false;
			}
		}
	}
	;

	function isNum(num) {
		if (num < 10) {
			return '0' + num;
		} else {
			return num;
		}
	}
	;

	function showClik() {
		oCal.find('td').click(
				function() {
					if ($(this).attr('class') == 'blue'
							|| $(this).attr('class') == 'red') {
						var isBan = $(this).find("span").html();
						var _isBan = "";
						var th = $(this);
						var workDate = $(this).attr('date');
						var doctorId = $("#doctorId").val();
						if(isBan){
							_isBan = "0";
						}else{
							_isBan = "1";
						}
						$.ajax({
							type:"post",
							url:ctx+"/doctorWork/workDate",
							data:{"doctorId":doctorId,
								"isBan":_isBan,
								"workDate":workDate},
							dataType:"json",
							success:function(data){
								if(data > 0){
									if(_isBan == "0"){
										$(th).find("span").html("");
									}else{
										$(th).find("span").html("班");
									}
								}
							}
						});
					}

				});
	}
	;
	showClik();
	$("#pp-layer-close").click(function() {
		$(".calendar").fadeOut();
		$(".modal-backdrop").fadeOut()
	});

});
